package com.cloudkinto.service.workOrder.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.bo.workOrder.reply.WorkOrderReplyAddBo;
import com.cloudkinto.common.BeanConvert;
import com.cloudkinto.dao.WorkOrderDao;
import com.cloudkinto.dao.WorkOrderReplyDao;
import com.cloudkinto.entity.WorkOrderDo;
import com.cloudkinto.entity.WorkOrderReplyDo;
import com.cloudkinto.service.file.FileService;
import com.cloudkinto.service.workOrder.WorkOrderReplyService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 工单回复 service实现类
 * </p>
 *
 * @author long
 * @since 2025-01-07
 */
@Service
@Transactional(rollbackFor = Exception.class)
@RequiredArgsConstructor
public class WorkOrderReplyServiceImpl extends ServiceImpl<WorkOrderReplyDao, WorkOrderReplyDo> implements WorkOrderReplyService {

    private final FileService fileService;

    private final WorkOrderDao workOrderDao;

    @Override
    public int add(WorkOrderReplyAddBo req, Long userId, String source) {
//        WorkOrderReplyDo entityDo = new WorkOrderReplyDo();
//        BeanUtils.copyProperties(req, entityDo);
        WorkOrderReplyDo entityDo = BeanConvert.INSTANCE.workOrderReplyAddBoToWorkOrderReplyDo(req);
        entityDo.setUserId(userId);
        entityDo.setOrigin("oms".equals(source) ? 1 : 2);
        if (StrUtil.isNotBlank(req.getFileIds())) {
            fileService.uploadFileMult(req.getFileIds().split(","));
        }
        WorkOrderDo workOrderDo = new WorkOrderDo();
        workOrderDo.setId(req.getWorkOrderId());
        workOrderDo.setCustomerReception("oms".equals(source) ? Boolean.FALSE : Boolean.TRUE);
        workOrderDao.updateById(workOrderDo);
        return baseMapper.insert(entityDo);
    }

}
